from collections import OrderedDict

class vdict(dict): # need to extend dict so it can be passed as an argument to exec()
	"""
	vdict: 一个字典类，它有get、set和del事件的回调
	"""
	def __init__(self, *args, getitem=None, setitem=None, delitem=None, **kwargs):
		self.store = OrderedDict()
		self.store.update(dict(*args, **kwargs))
		self._get = getitem
		self._del = delitem
		self._set = setitem
		self.keys = self.store.keys
		self.values = self.store.values

	def __getitem__(self, key):
		if self._get:
			return self._get(key)
		return self.store[key]

	def __setitem__(self, key, value):
		if self._set:
			ret = self._set(key, value)
			if ret is False:
				return
		self.store[key] = value

	def __delitem__(self, key):
		if self._del:
			ret = self._del(key)
			if ret is False:
				return
		del self.store[key]

	def __contains__(self, key):
		return key in self.store

	def __iter__(self):
		return iter(self.store)

	def __len__(self):
		return len(self.store)

	def __repr__(self):
		return repr(self.store)